/**
 * This Class is used to perform the interaction with the database.
 * This class will be called only from the server to execute queries and to return results from the database.
 * 
 * @author Gil Levinson
 * @author Boris Fradlis
 * @author Yogev Levi
 * @author Hila Porat
 *  
 */

package vcp.fixtures;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Seconds;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import fit.ActionFixture;

import system.SystemMode;
import system.SystemTypes;
import vcpClasses.*;

import java.io.Serializable;
import java.sql.*;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;


//Start of DataBaseConnection

public class DataBaseConnection extends ActionFixture {
	
	private DateTime currDate;
	private DateTime orderDate;
	private float cost;
	public DataBaseConnection(){}
	
	/**
	 * this method transfer string to type of DateTime
	 * @param s string in format dd/mm/yyyy hh:mm:ss
	 * @return DateTime date
	 */
	public DateTime StrToDate(String s)
	{
		String[] dateStr;
		String datesplit;
		String time;
		dateStr=s.split(" ");
		datesplit = dateStr[0];
		time=dateStr[1];
		DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
		DateTime date = formatter.parseDateTime(datesplit + " " +time);
		return date;
		
	}
	
	public void setCurrentDate(String s)
	{
		currDate=StrToDate(s);
	}
	public void setOrderDate(String s)
	{
		orderDate=StrToDate(s);
	}
	public void setCost(Float n)
	{
		cost= n;
	}
	/**
	 * this method calculate the price for cancel Order
	 * @return the cost for the cancel
	 */
	public float cancelOrder()
	{
		
		int diff = Minutes.minutesBetween(currDate, orderDate).getMinutes();
		if (diff <180 && diff >=60 )
			cost = (float) (0.5*cost);
		else if (diff > 60)
			cost = 0;
		return cost;

	}
	/**
	 * this method calculate the price for late Order
	 * @return the cost for the late
	 */
	public float calculateFine(){
		float diff = Minutes.minutesBetween(currDate,orderDate).getMinutes();
		if (diff<0)
			cost = (float) (1.2*cost);
		return cost;
	}
}